package fr.fbhelp.parsing
{
	import fr.fbhelp.config.Config;
	import fr.fbhelp.events.ParserEvent;
	import fr.fbhelp.files.FileManager;
	
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;
	
	public class ParseManager
	{
		public static var TITLE_COPY_HELP_DIRS:String = "Step 2: please confirm copying of the several help directories";
		public static var TITLE_DELETE_INDEX:String = "Step 3: Now, let's delete the eclipse help index folder";
		
		private var packageStore:HelpPackageStore = new HelpPackageStore(); 
		public var config:Config = new Config(); 
		
		public function ParseManager()
		{
			super();
		}
		
		/**
		 * Parse XML Config for Items:
		 * <package
		 * id="en_US_Flex_4.0_UsingFlashBuilder"
		 * label="Using Adobe Flash Builder 4"
		 * path="en_US/Flex/4.0/UsingFlashBuilder"
		 * contextSensitivity="yes"
		 * downloadContent="client.Flex_4.0_UsingFlashBuilder_en-us.zip"
		 * downloadContentDefault="yes"
		 * />
		 */
		public function parseXMLConfig(xmlString:String):void
		{
			var xml:XML = new XML(xmlString);
			var packages:XMLList = xml.descendants("package");
			Output.append("\tPackages found: " + packages.length());
			
			for each (var p:XML in packages)
			{
				packageStore.addPackageFromXML(p);
			}
		}
		
		public function readHelpPackages(fileUtil:FileManager): void
		{
			for each (var p:HelpPackage in packageStore.packages)
			{
				var tocContents:Toc = fileUtil.getTocForPackage(p);
				p.setPackageContents(p, tocContents);
			}
			packageStore.removeInexistentPackages();
			packageStore.createTocFromContents();
			packageStore.createTopLevelToc(fileUtil.path.baseEclipseHelpFolder.nativePath, fileUtil.path.nativeSeparator, fileUtil);
		}
		
		public function showOpeningScreen(): void
		{
			Output.append("Initializing application...");
			Alert.show("This Friendly Flash Builder Docs creates standard Eclipse help from the Flash Builder documentation which comes with Adobe's Community Help Center (CHC). " +
				"To make it, the following steps must be done:\n\n" +
				"1. Create standard Eclipse's 'toc.xml' TOC and save it it.\n" +
				"2. Copy local help html files to the Eclipse's standard help directory.\n" +
				"3. Clean the Eclipse help index by removing the index directory.\n\n" +
				"" +
				"Application will do these steps automatically. " +
				"After the process will be finished, please restart the Flash Builder to see the new help contents.\n\n" +
				"Then, you can access the help through standard Eclipse's help search and in Help > Eclipse Help. During the first search, Eclipse help will be re-indexed to make new contents searchable.\n\n" +
				"Before proceeding, please make sure you have launched Flash Builder Help at least one time before and have some local help content downloaded through it. " +
				"Also, please close the Flash Builder to make file operations possible.\n\n" +
				"To start processing, click 'OK' button."
				
				, "Hello, Friendly Flash Builder Docs", (Alert.OK | Alert.CANCEL), null, onCloseOpeningScreen, null, Alert.OK); 
		}
		
		private function onCloseOpeningScreen(event:CloseEvent): void
		{
			if (event.detail == Alert.OK)
			{
				FlexGlobals.topLevelApplication.dispatchEvent(new ParserEvent(ParserEvent.PARSER_START));
			}
			if (event.detail == Alert.CANCEL)
			{
				Output.append("Help creation cancelled, please restart application if you want to try again.");
			}
		}
		
		public function showClosingScreen(): void
		{
			Alert.show("Eclipse Help Index was cleaned. Now, please restart the Flash Builder to see the new help contents " +
				"in the standard Eclipse's help search and in Help > Eclipse Help. " +
				"During the first search, Eclipse help will be re-indexed to make new contents searchable.\n\n" +
				"This application can be closed now."
				, "All done", Alert.OK); 
		}
	}
}